﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using DotNetApp.Toolkit.Requests;
using WinSource.Model;

namespace WinSource.Client
{
    public class GetVideosRequestArgs : RequestArgs
    {
        #region Constructor

        public GetVideosRequestArgs(bool isSuccess, uint responseTimeInMs)
            : base(isSuccess, responseTimeInMs)
        {
            Videos = new List<Video>();
        }

        #endregion

        #region Properties

        public List<Video> Videos { get; set; }

        #endregion
    }

    internal class GetVideosRequest : Request<GetVideosRequestArgs>
    {
        #region Constants

        private const string Url = "http://gdata.youtube.com/feeds/api/users/WinSourceVideo/uploads";

        #endregion

        #region Constructor

        public GetVideosRequest(Action<GetVideosRequestArgs> callback = null)
            : base(callback)
        {
        }

        #endregion

        #region Properties

        protected override string Link
        {
            get { return Url; }
        }

        #endregion

        #region Request Overrides

        protected override void OnRequestCompleted(RequestResponseEventArgs requestResponseEventArgs)
        {
            if (!requestResponseEventArgs.Cancelled)
            {
                GetVideosRequestArgs getVideosRequestArgs = new GetVideosRequestArgs(requestResponseEventArgs.Error == null, ResponseTimeInMs);

                if (requestResponseEventArgs.Error == null)
                {
                    try
                    {
                        XDocument xDocument = XDocument.Parse(requestResponseEventArgs.Result);

                        foreach (XNode xNode in xDocument.DescendantNodes())
                        {
                            if (xNode.ToString().StartsWith("<entry"))
                            {
                                XElement entryElement = (XElement)xNode;

                                string url = string.Empty;
                                string title = string.Empty;
                                DateTime publishDate = new DateTime();
                                
                                foreach (XNode elementNode in entryElement.DescendantNodes())
                                {
                                    XElement subElement = elementNode as XElement;

                                    if (subElement != null)
                                    {
                                        switch (subElement.Name.LocalName)
                                        {
                                            case "id":
                                                url = subElement.Value;
                                                break;

                                            case "published":
                                                DateTime.TryParse(subElement.Value, out publishDate);
                                                break;

                                            case "title":
                                                title = subElement.Value;
                                                break;
                                        }
                                    }
                                }

                                getVideosRequestArgs.Videos.Add(new Video(title, url, publishDate));
                            }
                        }
                    }
                    catch
                    {
                    }

                    InvokeCallback(getVideosRequestArgs);
                }
            }
        }

        #endregion
    }
}